package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.bean.*
import com.rjkj.medicalwaste.databinding.ItemOutboundListBinding

class OutboundListAdapter(var list: List<WaitHandoverGarbageBean>, val context: Context) : RecyclerView.Adapter<OutboundListAdapter.ViewHolder>() {

    class ViewHolder(private val binding: ItemOutboundListBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: WaitHandoverGarbageBean, listener: View.OnClickListener) {
            MedicalWasteType.getTypeByTypeName(item.garbageBoxType).let {
                binding.tvType.text = it.alias
                if (it.bgId != 0)
                    binding.tvType.setBackgroundResource(it.bgId)
            }
            binding.tvWeight.text = item.garbageBoxWeight.toString()
            item.garbageBoxTime.split(" ").let {
                if (it.size >= 2) {
                    binding.tvTimeYmd.text = it[0].substring(2, it[0].length)
                    binding.tvTimeHms.text = it[1]
                }
            }
            binding.btChoose.isSelected = item.myCheck
            binding.root.setOnClickListener(listener)
            binding.executePendingBindings()
        }
    }
    fun setBoxChoose(code:String){
        for (s in list) {
            if (code == s.garbageBoxQRCode) {
                s.myCheck = true
                break
            }
        }
        notifyDataSetChanged()
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val test = list[position]
        holder.bind(test) {
            test.myCheck = !test.myCheck
            notifyItemChanged(position)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemOutboundListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}